<?php
/**
 * Created by PhpStorm.
 * User: pt
 * Date: 2018/8/21
 * Time: 13:55
 */

namespace app\api\controller;
use think\Db;

/**
 * use   文件
 * Class Upload
 * @package app\api\controller
 */
class Upload
{
    //上传图片
    public function up()
    {

        //验证
        if(!isset($_FILES["file"])) {
            return json(array('code' => 1,'text'=>"没有文件"));
        }

        //文件信息
        $file_path = $_FILES["file"]["tmp_name"];
        $file_size = $_FILES["file"]["size"];
        $file_type = $_FILES["file"]["type"];
        $file_name = $_FILES["file"]["name"];
        $arr = pathinfo($file_name);
        if(isset($arr['extension'])){
            $ext = $arr['extension'];
        } else {
            $ext = "";
        }


        //创建文件夹
        $root = ROOT_PATH . "public";
        $path = "/upload/".date("Y/m/d/");
        if (!file_exists($root.$path)){
            mkdir ($root.$path,0777,true);
        }

        $url = $path . uuid() . (empty($ext)?"":".".$ext);

        copy($file_path,$root.$url);

        $arr = array();
        $arr["file_name"] = $file_name;
        $arr["file_type"] = $file_type;
        $arr["file_size"] = $file_size;
        $arr["file_ext"] = $ext;
        $arr["file_url"] = $url;
        return json(array('code' => 0,'data'=>$arr));

    }


    //上传文件
    public function upfile()
    {

        $rel = input('rel');

        $key = input('key','');

        //验证
        if(!isset($_FILES["file"])) {
            return errorMsg('没有文件');
        }

        if($_FILES["file"]['error'] > 0){
            return errorMsg();
        }


        //文件信息
        $file_path = $_FILES["file"]["tmp_name"];
        $file_size = $_FILES["file"]["size"];
        $file_type = $_FILES["file"]["type"];
        $file_name = $_FILES["file"]["name"];
        $arr = pathinfo($file_name);
        $file_name_out = $arr['filename'];
        if(isset($arr['extension'])){
            $ext = $arr['extension'];
        } else {
            $ext = "";
        }

        //创建文件夹
        $root = ROOT_PATH . "public";
        $path = "/upload/".date("Y/m/d/");
        if (!file_exists($root.$path)){
            mkdir ($root.$path,0777,true);
        }


        //罗技英文系统 文件编码没搞定 暂时用这种
        $xpath = $path . uuid() .'_'.$key. (empty($ext)?"":".".$ext);

        copy($file_path,$root.$xpath);

        $arr = array();
        $arr["file_name"] = $file_name;
        $arr["file_type"] = $file_type;
        $arr["file_size"] = $file_size;
        $arr["file_ext"] = $ext;
        $arr["file_url"] = $xpath;
        $arr["rel"] = $rel;

        Db::table('bus_upload')->insert($arr);

        return json(array('code' => 0,'data'=>$arr));

    }

    // IQC 上传图片 修改
    public function upfiles()
    {

        $rel = input('rel');

        //验证
        if(!isset($_FILES["file"])) {
            return json(array('code' => 1,'text'=>"没有文件"));
        }

        if($_FILES["file"]['error'] > 0){
            return json(array('code' => 1,'text'=>"上传文件出错，出错代码：" . $_FILES["file"]['error']));
        }

        //文件信息
        $file_path = $_FILES["file"]["tmp_name"];
        $file_size = $_FILES["file"]["size"];
        $file_type = $_FILES["file"]["type"];
        $file_name = $_FILES["file"]["name"];
        $arr = pathinfo($file_name);
        $file_name_out = $arr['filename'];
        if(isset($arr['extension'])){
            $ext = $arr['extension'];
        } else {
            $ext = "";
        }
        $ext = strtolower($ext);
        if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'gif' ||$ext == 'png'){

        }else{
            return json(array('code' => 1,'text'=>"图片格式不正确"));
        }
        //创建文件夹
        $root = ROOT_PATH . "public";
        $path = "/upload/".date("Y/m/d/");
        if (!file_exists($root.$path)){
            mkdir ($root.$path,0777,true);
        }


        //罗技英文系统 文件编码没搞定 暂时用这种
        $xpath = $path . uuid() . (empty($ext)?"":".".$ext);

        copy($file_path,$root.$xpath);

//
        $arr = array();
        $arr["file_name"] = $file_name;
        $arr["file_type"] = $file_type;
        $arr["file_size"] = $file_size;
        $arr["file_ext"] = $ext;
        $arr["file_url"] = $xpath;
        $arr["rel"] = $rel;

        Db::table('bus_upload')->insert($arr);

        return json(array('code' => 0,'data'=>$arr));

    }



    //删除文件
    public function delfile()
    {
        $id = input('id/d',0);

        $row = Db::table('bus_upload')->where(['id'=>$id])->find();

        if(isset($row)){
            $root = ROOT_PATH . "public";

            $path = $root . iconv('utf-8', 'gb2312',$row['file_url']);

            if(file_exists($path)){
                unlink($path);
            }

            Db::table('bus_upload')->where(['id'=>$id])->delete();

            return json(array('code' => 0,'text'=>"删除成功"));

        }
        else {
            return json(array('code' => 1,'text'=>"删除失败！没有此文件"));
        }
    }

    //下载文件
    public function download(){
        $id = input('id/d',0);
        $upload = Db::table("bus_upload")->where(["id"=>$id])->field('file_url,file_name')->find();

        if(empty($upload)){
            return json(['code'=>1,'text'=>'no data']);
        }

        $root = ROOT_PATH . "public";
        $path = $root.str_replace('/',"\\", $upload['file_url']);
        $name = $upload['file_name'];

        if(!file_exists($path)){
            return json(['code'=>1,'text'=>'no file']);
        }

        ob_clean();
        Header( "Content-type:  application/octet-stream ");
        Header( "Accept-Ranges:  bytes ");
        Header( "Accept-Length: " .filesize($path));
        header( "Content-Disposition:  attachment;  filename= {$name}");
        echo file_get_contents($path);
        readfile($path);
        die();
    }
}